var mrTabf = {
        select : function(e){
                var $t = $(this);
                if(!$t.hasClass('mrTabSelected')){
                        $s = $t.siblings('.mrTabSelected');
                        $s.toggleClass('mrTabSelected');
                        $('#' + $s.attr('data-tab')).hide();
                        $t.trigger('stopBlink');
                        $t.toggleClass('mrTabSelected');
                        $('#' + $t.attr('data-tab')).show();
                }
        },
        close: function(e){
                var $b = $(this).parent();
                var $tabs = $b.parent();
                if($tabs.hasClass('mrTabs')){
                        //remove tab container
                        $('#' + $b.attr('data-tab')).remove();
                        $b.remove();

                        //if there is not a tab selected, select one of them
                        try{
                                if($tabs.children('.mrTabSelected').length == 0){
                                        $tabs.children('.mrTabSelector').first().click();
                                }
                        }catch(e){}
                }
                e.stopPropagation();
        }
};

$(function(){
        //console.log('tabs ' + $('.mrTabPanel').length);
        
        $('.mrTabContainer').hide();
        
        $('.mrTabSelected').each(function(){
                $('#' + $(this).attr('data-tab')).show();
        });
        
        $('.mrTabSelector').each(function(){
                var $this = $(this);
                var cpt = $this.find('span.mrTabCaption').text();
                $this.attr('data-cap', cpt);
                if($this.find('span.mrCloseTab').length == 0){
                        $this.append('<span class="mrCloseTab">x</span>');
                }
                $this.find('span.mrCloseTab').click(mrTabf.close);
                $this.bind('click', mrTabf.select);
        });
        
        /*$('.mrCloseTab').live('click', function(e){
                var $b = $(this).parent();
                var $tabs = $b.parent();
                if($tabs.hasClass('mrTabs')){
                        //remove tab container
                        $('#' + $b.attr('data-tab')).remove();
                        $b.remove();

                        //if there is not a tab selected, select one of them
                        try{
                                if($tabs.children('.mrTabSelected').length == 0){
                                        $tabs.children('.mrTabSelector').first().click();
                                }
                        }catch(e){}
                }
                e.stopPropagation();
        });*/
        
        $('.mrTabSelector').live('blink', function(){
                var $t = $(this);
                
                $t.trigger('stopBlink');
                
                if(!$t.hasClass('mrTabSelected')){
                        var interval = setInterval(function(){
                                $t.toggleClass('mrRedBlink');
                        }, 500);
                        $t.data('bl', interval);
                        var tmr = setTimeout(function(){
                                clearTimeout(interval);
                                $t.addClass('mrRedBlink');
                        }, 20000); //always interval and timeOut must to be multiplo each other
                        $t.data('tmr', tmr);
                }
        });
        
        $('.mrTabSelector').live('stopBlink', function(){
                var $t = $(this);
                if(!$t.hasClass('mrTabSelected')){
                        var interval = $t.data('bl');
                        clearTimeout(interval);
                        var tmr = $t.data('tmr');
                        clearTimeout(tmr);
                        $t.removeClass('mrRedBlink');
                }
        });
});



function mrTab (tabPanel) {
        this.$tab = tabPanel;
        //lets construct tabPanel internal structure
        if(this.$tab.children('.mrTabs').length < 1){
                this.$tab.html('<div class="mrTabs"></div><div class="mrTabContainers"></div>');
        }
        
        this.$s = this.$tab.children('.mrTabs');
        this.$c = this.$tab.children('.mrTabContainers');

        this.addTab = function(caption, id){
                var noSpaceName;
                if(id){
                        if($('#' + id).length > 0){
                                return;
                        }
                        noSpaceName = id;
                }else{
                        noSpaceName = caption.replace(/ /g, '') + '-' + $('.mrTabContainer').length;
                }
                //'" style="margin-right: 0px;">
                $ts = $('<div data-tab="' + noSpaceName + '" class="mrTabSelector" style="margin-right: 0px;" data-cap="' + caption + '"><span class="mrTabCaption">' + caption + '</span><span class="mrCloseTab">x</span></div>');
                this.$s.append($ts);
                $ts.bind('click', mrTabf.select);
                $ts.find('span.mrCloseTab').click(mrTabf.close);
                $div = $('<div id="' + noSpaceName + '" class="mrTabContainer"></div>');
                this.$c.append($div);
                if(this.$c.children('.mrTabContainer').length > 1) {
                        $div.hide();
                }else{
                        $ts.addClass('mrTabSelected');
                }
                return $div;
        };
        
        /* this function deletes all tabs in the tab panel */
        this.deleteAllTabs = function(){
                this.$tab.html('<div class="mrTabs"></div><div class="mrTabContainers"></div>');
                this.$s = this.$tab.children('.mrTabs');
                this.$c = this.$tab.children('.mrTabContainers');
        };
        
        
        this.deleteTabsByCaption = function(caption){
                this.$s.children('.mrTabSelector[data-cap="' + caption + '"]').each(function(){
                //      $('#' + $(this).attr('data-tab')).remove();
                        $(this).children('span.mrCloseTab').click(); //remove();
                });
                
                /*try{
                        if(this.$s.children('.mrTabSelected').length == 0){
                                this.$s.children().first().click();
                        }       
                }catch(e){}*/
        };
        
        this.deleteTabsById = function(id){
                this.$s.children('.mrTabSelector[data-tab="' + id + '"]').children('span.mrCloseTab').click();
                /*$('#' + id).remove();
                
                try{
                        if(this.$s.children('.mrTabSelected').length == 0){
                                this.$s.children().first().click();
                        }       
                }catch(e){}*/
        };
        
        this.setSelectedTabByCaption = function(caption, index){
                this.getTabSelectorByCaption(caption, index).click();
        };
        
        this.setSelectedTabById = function(id){
                this.getTabSelectorById(id).click();
        };
        
        this.setSelectedTabByIndex = function(index){
                this.$s.children().eq(index).click();
        };
        
        this.getTabSelectorByIndex = function(index){
                return this.$s.children().eq(index);
        };
        
        this.getTabSelectorById = function(id){
                return this.$s.children('.mrTabSelector[data-tab="' + id + '"]');
        };
        
        this.getTabSelectorByCaption = function(caption, index){
                if(isNaN(index)){
                        return this.$s.children('.mrTabSelector[data-cap="' + caption + '"]').first();
                }else{
                        return this.$s.children('.mrTabSelector[data-cap="' + caption + '"]').eq(index);
                }
        };
        
        this.getTabContainerById = function(id){
                return $('#'+id);
        };
        
};
